/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ImagenesDBGad.GUI;

import ImagenesDBGad.Controladores.CargaDatosScript;
import ImagenesDBGad.Controladores.ControladorPersistencia;
import ImagenesDBGad.GUI.Icons.UtilidadIconos;
import ImagenesDBGad.IO.Imagen;
import Utils.ArchUtils;
import Utils.SVGUtils;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Ivan Pianetti @BIPiane
 * http://code.google.com/p/imagenesdb-gad/source/browse/
 */
public class SVGFrameGAD extends javax.swing.JFrame {
    
    private Imagen imgSVGPrincipal = new Imagen();
    private Imagen imgSVGResultado = new Imagen();
    private Map<Integer, ObjetoResultadoVista> mapObjResultado = new HashMap<Integer, ObjetoResultadoVista>();

    /**
     * Creates new form SVGFrameGAD
     */
    public SVGFrameGAD() {
        initComponents();
        ControladorPersistencia.getInstance().getEntity();
        ControladorPersistencia.getInstance().cargarScriptDB();
        
        this.setIconos();
        jButtonDescargar.setEnabled(false);
        jButtonBuscarEnDB.setEnabled(false);
        jButtonGuardarDB.setEnabled(false);
        
        jTableResultados.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            
            @Override
            public void valueChanged(ListSelectionEvent lse) {
                if (!lse.getValueIsAdjusting()) {
                    if (!mapObjResultado.isEmpty()) {
                        Integer i = jTableResultados.getSelectedRow() + 1;
                        
                        ObjetoResultadoVista obj = mapObjResultado.get(i);
                        if (obj != null) {
                            imgSVGResultado = new Imagen();
                            imgSVGResultado.setCodXML(obj.getXmlImagen());
                            imgSVGResultado.setNombre(obj.getNombre());
                            
                            ArchUtils.crearSVGAuxiliar(imgSVGResultado);
                            jSVGCanvasResultado.setURI("file:" + ArchUtils.urlSVGAuxiliar);
                            String a = jSVGCanvasResultado.getURI();
                            
                            jButtonDescargar.setEnabled(true);
                        }
                    } else {
                        jButtonDescargar.setEnabled(false);
                        Toolkit.getDefaultToolkit().beep();
                    }
                }
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrameInfoDataBase = new javax.swing.JFrame();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextFiel1 = new javax.swing.JTextField();
        jTextFieldCantidadImagenes = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldCantidadPivotes = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldPromedioTiempo = new javax.swing.JTextField();
        jButtonCerrarInfoDB = new javax.swing.JButton();
        jMenuBar3 = new javax.swing.JMenuBar();
        jMenuInfo3 = new javax.swing.JMenu();
        jFrameAcercaDe = new javax.swing.JFrame();
        jLabelCaratula = new javax.swing.JLabel();
        jButtonCerrarAcercaDe = new javax.swing.JButton();
        jMenuBar4 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jButtonAbrirSVG = new javax.swing.JButton();
        jSVGCanvasPrincipal = new org.apache.batik.swing.JSVGCanvas();
        jButtonBuscarEnDB = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableResultados = new javax.swing.JTable();
        jSVGCanvasResultado = new org.apache.batik.swing.JSVGCanvas();
        jLabel4 = new javax.swing.JLabel();
        jButtonDescargar = new javax.swing.JButton();
        jButtonGuardarDB = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenuImagenesDB_GAD = new javax.swing.JMenu();
        jMenuItemCargarDB = new javax.swing.JMenuItem();
        jMenuItemSalir = new javax.swing.JMenuItem();
        jMenuAyuda = new javax.swing.JMenu();
        jMenuItemInfoDataBase = new javax.swing.JMenuItem();
        jMenuItemVerManual = new javax.swing.JMenuItem();
        jMenuItemAcercaDe = new javax.swing.JMenuItem();

        jLabel1.setText("Base de Datos:");

        jLabel2.setText("Cantidad de Imagenes:");

        jTextFiel1.setEditable(false);
        jTextFiel1.setText("ImagenesDB-GAD");

        jTextFieldCantidadImagenes.setEditable(false);

        jLabel3.setText("Cantidad de Pivotes:");

        jTextFieldCantidadPivotes.setEditable(false);

        jLabel5.setText("Tiempo promedio busqueda:");

        jTextFieldPromedioTiempo.setEditable(false);

        jButtonCerrarInfoDB.setText("Cerrar");
        jButtonCerrarInfoDB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCerrarInfoDBActionPerformed(evt);
            }
        });

        jMenuInfo3.setText("Información Data Base");
        jMenuBar3.add(jMenuInfo3);

        jFrameInfoDataBase.setJMenuBar(jMenuBar3);

        org.jdesktop.layout.GroupLayout jFrameInfoDataBaseLayout = new org.jdesktop.layout.GroupLayout(jFrameInfoDataBase.getContentPane());
        jFrameInfoDataBase.getContentPane().setLayout(jFrameInfoDataBaseLayout);
        jFrameInfoDataBaseLayout.setHorizontalGroup(
            jFrameInfoDataBaseLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jFrameInfoDataBaseLayout.createSequentialGroup()
                .add(26, 26, 26)
                .add(jFrameInfoDataBaseLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jFrameInfoDataBaseLayout.createSequentialGroup()
                        .add(jLabel5)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jTextFieldPromedioTiempo))
                    .add(jFrameInfoDataBaseLayout.createSequentialGroup()
                        .add(jFrameInfoDataBaseLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jFrameInfoDataBaseLayout.createSequentialGroup()
                                .add(46, 46, 46)
                                .add(jLabel3))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel2)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel1))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jFrameInfoDataBaseLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jTextFiel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 147, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jFrameInfoDataBaseLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                .add(jTextFieldCantidadPivotes)
                                .add(jTextFieldCantidadImagenes, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 144, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(46, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jFrameInfoDataBaseLayout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jButtonCerrarInfoDB)
                .add(150, 150, 150))
        );
        jFrameInfoDataBaseLayout.setVerticalGroup(
            jFrameInfoDataBaseLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jFrameInfoDataBaseLayout.createSequentialGroup()
                .addContainerGap()
                .add(jFrameInfoDataBaseLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jTextFiel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jFrameInfoDataBaseLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jTextFieldCantidadImagenes, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2))
                .add(9, 9, 9)
                .add(jFrameInfoDataBaseLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(jTextFieldCantidadPivotes, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(6, 6, 6)
                .add(jFrameInfoDataBaseLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(jTextFieldPromedioTiempo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jButtonCerrarInfoDB)
                .addContainerGap(11, Short.MAX_VALUE))
        );

        jButtonCerrarAcercaDe.setText("Cerrar");
        jButtonCerrarAcercaDe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCerrarAcercaDeActionPerformed(evt);
            }
        });

        jMenu1.setText("Acerca de ImagenesDB-GAD");
        jMenuBar4.add(jMenu1);

        jFrameAcercaDe.setJMenuBar(jMenuBar4);

        org.jdesktop.layout.GroupLayout jFrameAcercaDeLayout = new org.jdesktop.layout.GroupLayout(jFrameAcercaDe.getContentPane());
        jFrameAcercaDe.getContentPane().setLayout(jFrameAcercaDeLayout);
        jFrameAcercaDeLayout.setHorizontalGroup(
            jFrameAcercaDeLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jFrameAcercaDeLayout.createSequentialGroup()
                .add(233, 233, 233)
                .add(jButtonCerrarAcercaDe)
                .addContainerGap())
            .add(jLabelCaratula, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 567, Short.MAX_VALUE)
        );
        jFrameAcercaDeLayout.setVerticalGroup(
            jFrameAcercaDeLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jFrameAcercaDeLayout.createSequentialGroup()
                .add(jLabelCaratula, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 10, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButtonCerrarAcercaDe)
                .add(12, 12, 12))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButtonAbrirSVG.setText("Abrir SVG");
        jButtonAbrirSVG.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAbrirSVGActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jSVGCanvasPrincipalLayout = new org.jdesktop.layout.GroupLayout(jSVGCanvasPrincipal);
        jSVGCanvasPrincipal.setLayout(jSVGCanvasPrincipalLayout);
        jSVGCanvasPrincipalLayout.setHorizontalGroup(
            jSVGCanvasPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 324, Short.MAX_VALUE)
        );
        jSVGCanvasPrincipalLayout.setVerticalGroup(
            jSVGCanvasPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 252, Short.MAX_VALUE)
        );

        jButtonBuscarEnDB.setText("Buscar en DB");
        jButtonBuscarEnDB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarEnDBActionPerformed(evt);
            }
        });

        jTableResultados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nro", "Nombre", "Cant. Obj", "Similitud %"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTableResultados);

        org.jdesktop.layout.GroupLayout jSVGCanvasResultadoLayout = new org.jdesktop.layout.GroupLayout(jSVGCanvasResultado);
        jSVGCanvasResultado.setLayout(jSVGCanvasResultadoLayout);
        jSVGCanvasResultadoLayout.setHorizontalGroup(
            jSVGCanvasResultadoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 291, Short.MAX_VALUE)
        );
        jSVGCanvasResultadoLayout.setVerticalGroup(
            jSVGCanvasResultadoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 252, Short.MAX_VALUE)
        );

        jLabel4.setText("=>");

        jButtonDescargar.setText("Descargar");
        jButtonDescargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDescargarActionPerformed(evt);
            }
        });

        jButtonGuardarDB.setText("Agregar a DB");
        jButtonGuardarDB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGuardarDBActionPerformed(evt);
            }
        });

        jMenuImagenesDB_GAD.setText("ImagenesDB-GAD");

        jMenuItemCargarDB.setText("Cargar DB");
        jMenuItemCargarDB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemCargarDBActionPerformed(evt);
            }
        });
        jMenuImagenesDB_GAD.add(jMenuItemCargarDB);

        jMenuItemSalir.setText("Salir");
        jMenuItemSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemSalirActionPerformed(evt);
            }
        });
        jMenuImagenesDB_GAD.add(jMenuItemSalir);

        jMenuBar1.add(jMenuImagenesDB_GAD);

        jMenuAyuda.setText("Ayuda");

        jMenuItemInfoDataBase.setText("Info Data Base");
        jMenuItemInfoDataBase.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemInfoDataBaseActionPerformed(evt);
            }
        });
        jMenuAyuda.add(jMenuItemInfoDataBase);

        jMenuItemVerManual.setText("Ver Informe");
        jMenuItemVerManual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemVerManualActionPerformed(evt);
            }
        });
        jMenuAyuda.add(jMenuItemVerManual);

        jMenuItemAcercaDe.setText("Acerca de ImagenesDB-GAD");
        jMenuItemAcercaDe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemAcercaDeActionPerformed(evt);
            }
        });
        jMenuAyuda.add(jMenuItemAcercaDe);

        jMenuBar1.add(jMenuAyuda);

        setJMenuBar(jMenuBar1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(357, 357, 357)
                        .add(jSVGCanvasResultado, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 291, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(29, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(jSVGCanvasPrincipal, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE)
                            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE))
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(33, 33, 33)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                                    .add(jButtonGuardarDB)
                                    .add(jButtonBuscarEnDB)
                                    .add(jButtonAbrirSVG, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 115, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                            .add(layout.createSequentialGroup()
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel4)))
                        .add(0, 205, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(0, 391, Short.MAX_VALUE)
                        .add(jButtonDescargar)
                        .add(205, 205, 205))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jSVGCanvasPrincipal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 252, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(77, 77, 77)
                        .add(jButtonAbrirSVG)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonBuscarEnDB)
                        .add(5, 5, 5)
                        .add(jButtonGuardarDB)))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .add(jSVGCanvasResultado, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButtonDescargar)
                        .addContainerGap(22, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(0, 100, Short.MAX_VALUE)
                        .add(jLabel4)
                        .add(189, 189, 189))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonAbrirSVGActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAbrirSVGActionPerformed
        String url = ArchUtils.seleccionarArchivo();
        if (!url.equalsIgnoreCase("")) {
            jSVGCanvasPrincipal.setURI(url);
            imgSVGPrincipal = SVGUtils.cargarImagen(url);
            jButtonBuscarEnDB.setEnabled(true);
            jButtonGuardarDB.setEnabled(true);
        }
    }//GEN-LAST:event_jButtonAbrirSVGActionPerformed
    
    private void jButtonBuscarEnDBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarEnDBActionPerformed
        ArrayList<Object[]> listaImagResult = ControladorPersistencia.getInstance().buscarImagenes(imgSVGPrincipal);
        DefaultTableModel model = (DefaultTableModel) jTableResultados.getModel();
        
        int cant = model.getRowCount();
        for (int i = 0; i < cant; i++) {
            model.removeRow(0);
        }
        
        int i = 1;
        mapObjResultado = new HashMap<Integer, ObjetoResultadoVista>();
        for (Object[] obj : listaImagResult) {
            ObjetoResultadoVista objResult = new ObjetoResultadoVista(obj);
            mapObjResultado.put(i, objResult);
            model.addRow(new Object[]{i, objResult.getNombre(), objResult.getCantidadObjs(), objResult.getDistancia()});
            i++;
        }
        jTableResultados.setModel(model);
    }//GEN-LAST:event_jButtonBuscarEnDBActionPerformed
    
    private void jMenuItemAcercaDeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemAcercaDeActionPerformed
        jFrameAcercaDe.pack();
        jFrameAcercaDe.setVisible(true);
    }//GEN-LAST:event_jMenuItemAcercaDeActionPerformed
    
    private void jMenuItemInfoDataBaseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemInfoDataBaseActionPerformed
        
        Object[] prop = ControladorPersistencia.getInstance().getDatosDB();
        
        String cantImg = prop[0].toString();
        String cantPiv = prop[1].toString();
        String promTiempo = prop[2].toString();
        
        jTextFieldCantidadImagenes.setText(cantImg);
        jTextFieldCantidadPivotes.setText(cantPiv);
        jTextFieldPromedioTiempo.setText(promTiempo + " ms.");
        
        jFrameInfoDataBase.pack();
        jFrameInfoDataBase.setVisible(true);
    }//GEN-LAST:event_jMenuItemInfoDataBaseActionPerformed
    
    private void jButtonGuardarDBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGuardarDBActionPerformed
        if (imgSVGPrincipal != null && imgSVGPrincipal.getCodXML() != null) {
            ControladorPersistencia.getInstance().guardar(imgSVGPrincipal);
//            jButtonBuscarEnDBActionPerformed(evt);
            JOptionPane.showMessageDialog(this, "La imagen ha sido agregada con éxito.", "Notificación", JOptionPane.INFORMATION_MESSAGE);
        } else {
            Toolkit.getDefaultToolkit().beep();
        }
    }//GEN-LAST:event_jButtonGuardarDBActionPerformed
    
    private void jMenuItemVerManualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemVerManualActionPerformed
        try {
            Desktop.getDesktop().open(new File("src/Documentacion/InformeGAD.pdf"));
        } catch (IOException ex) {
        }
    }//GEN-LAST:event_jMenuItemVerManualActionPerformed
    
    private void jMenuItemSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSalirActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jMenuItemSalirActionPerformed
    
    private void jButtonDescargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDescargarActionPerformed
        String url = ArchUtils.guardarComoArchivo(imgSVGResultado.getNombre() + ".jpg");
        ArchUtils.guardarComoJPG(imgSVGResultado, url);
    }//GEN-LAST:event_jButtonDescargarActionPerformed
    
    private void jMenuItemCargarDBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemCargarDBActionPerformed
        this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
        CargaDatosScript.cargarMultiplesImagenes();
        this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_jMenuItemCargarDBActionPerformed
    
    private void jButtonCerrarInfoDBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCerrarInfoDBActionPerformed
        jFrameInfoDataBase.setVisible(false);
    }//GEN-LAST:event_jButtonCerrarInfoDBActionPerformed
    
    private void jButtonCerrarAcercaDeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCerrarAcercaDeActionPerformed
        jFrameAcercaDe.setVisible(false);
    }//GEN-LAST:event_jButtonCerrarAcercaDeActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SVGFrameGAD.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SVGFrameGAD.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SVGFrameGAD.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SVGFrameGAD.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {
            
            @Override
            public void run() {
                new SVGFrameGAD().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAbrirSVG;
    private javax.swing.JButton jButtonBuscarEnDB;
    private javax.swing.JButton jButtonCerrarAcercaDe;
    private javax.swing.JButton jButtonCerrarInfoDB;
    private javax.swing.JButton jButtonDescargar;
    private javax.swing.JButton jButtonGuardarDB;
    private javax.swing.JFrame jFrameAcercaDe;
    private javax.swing.JFrame jFrameInfoDataBase;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabelCaratula;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenuAyuda;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuBar jMenuBar3;
    private javax.swing.JMenuBar jMenuBar4;
    private javax.swing.JMenu jMenuImagenesDB_GAD;
    private javax.swing.JMenu jMenuInfo3;
    private javax.swing.JMenuItem jMenuItemAcercaDe;
    private javax.swing.JMenuItem jMenuItemCargarDB;
    private javax.swing.JMenuItem jMenuItemInfoDataBase;
    private javax.swing.JMenuItem jMenuItemSalir;
    private javax.swing.JMenuItem jMenuItemVerManual;
    private org.apache.batik.swing.JSVGCanvas jSVGCanvasPrincipal;
    private org.apache.batik.swing.JSVGCanvas jSVGCanvasResultado;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTableResultados;
    private javax.swing.JTextField jTextFiel1;
    private javax.swing.JTextField jTextFieldCantidadImagenes;
    private javax.swing.JTextField jTextFieldCantidadPivotes;
    private javax.swing.JTextField jTextFieldPromedioTiempo;
    // End of variables declaration//GEN-END:variables

    private void setIconos() {
        jButtonAbrirSVG.setIcon(UtilidadIconos.abrirSVGIcon);
        jButtonDescargar.setIcon(UtilidadIconos.descargarIcon);
        jButtonGuardarDB.setIcon(UtilidadIconos.guardarDBIcon);
        jButtonBuscarEnDB.setIcon(UtilidadIconos.buscarIcon);
        jMenuAyuda.setIcon(UtilidadIconos.ayudaIcon);
        jMenuImagenesDB_GAD.setIcon(UtilidadIconos.imagenesDBGADIcon);
        jMenuItemAcercaDe.setIcon(UtilidadIconos.infoIcon);
        jMenuItemVerManual.setIcon(UtilidadIconos.manualPDFIcon);
        jMenuItemInfoDataBase.setIcon(UtilidadIconos.infoIcon);
        jMenuItemSalir.setIcon(UtilidadIconos.salirIcon);
        jMenuItemCargarDB.setIcon(UtilidadIconos.cargarMultiplesImagenes);
        jLabelCaratula.setIcon(UtilidadIconos.caratulaIcon);
        jButtonCerrarInfoDB.setIcon(UtilidadIconos.cerrarIcon);
        jButtonCerrarAcercaDe.setIcon(UtilidadIconos.cerrarIcon);
    }
    
    private static class ObjetoResultadoVista {
        
        private int id = -1;
        private String nombre = "-";
        private String xmlImagen = "-";
        private int cantidadObjs = -1;
        private Double distancia = -1.0;
        
        public ObjetoResultadoVista() {
        }
        
        private ObjetoResultadoVista(Object[] obj) {
            
            this.id = Integer.parseInt(obj[0].toString());
            this.nombre = obj[1].toString();
            this.xmlImagen = obj[2].toString();
            this.cantidadObjs = Integer.parseInt(obj[3].toString());
            this.distancia = Double.parseDouble(obj[4].toString());
            
        }
        
        public int getCantidadObjs() {
            return cantidadObjs;
        }
        
        public Double getDistancia() {
            return distancia;
        }
        
        public int getId() {
            return id;
        }
        
        public String getNombre() {
            return nombre;
        }
        
        public String getXmlImagen() {
            return xmlImagen;
        }
    }
}
